<?php

namespace app\admin\controller\post;

use app\common\controller\Backend;

class Cate extends Backend
{
    public function __construct()
    {
        parent::__construct();

        $this->CateModel = model('Post.Cate');
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    // 分类首页
    public function index()
    {

        $title = '分类列表';


        $count = $this->CateModel->count();

        $postlist = $this->CateModel->order('weigh', 'asc')->paginate(10);

        $pagestr = $postlist->render();
        // 赋值
        $this->assign([
            'postlist' => $postlist,
            'pagestr' => $pagestr,
            'start' => '',
            'end' => '',
            'keywords' => '',
            'count' => $count,
            'title' => $title
        ]);

        return $this->fetch();
    }

    // 新增分类
    public function add()
    {
        $title = '新增分类';

        if ($this->request->isPost()) {

            $catename = $this->request->param('catename',);

            $CateName = $this->CateModel->where('name', $catename)->find();


            if ($CateName) {
                $data = [
                    'code' => 0,
                    'msg' => '该分类已经存在，请重新输入'
                ];

                return json($data);
            }

            $data = $this->request->param();

            $AdminData = [
                'name' => $data['catename'],
                'weigh' => $data['weigh'],

            ];

            $res = $this->CateModel->save($AdminData);

            if ($res === FALSE) {
                $this->error($this->CateModel->getError());
                exit;
            } else {
                $this->success('添加成功', url('admin/post.cate/index'));
                exit;
            }
        }
        // 赋值
        $this->assign([
            'title' => $title
        ]);

        return $this->fetch();
    }

    //编辑
    public function edit()
    {
        $title = '修改分类';

        // 地址栏上的id
        $id = $this->request->param('id', 0);

        $admin = $this->CateModel->find($id);

        if (!$admin) {
            $this->error('用户不存在，请重新选择', url('admin/admin/index'));
            exit;
        }

        // 修改资料
        if ($this->request->isPost()) {
            $catename = $this->request->param('catename', '');

            $weigh = $this->request->param('weigh');

            // 封装更新数据的数组
            $data = [
                'id' => $id,
                'weigh' => $weigh,
                'name' => $catename,
            ];

            if (!empty($catename)) {
                $data['name'] = $catename;
            }

            $res = $this->CateModel->isUpdate(true)->save($data);

            if ($res === FALSE) {
                $this->error($this->CateModel->getError());
                exit;
            } else{
                $this->success('修改成功', url('admin/post.cate/index'));
            }
                
        }

        $this->assign([
            'title' => $title,
            'admin' => $admin,
            'info' => $admin

        ]);
        return $this->fetch();
    }

    // 删除
    public function delete()
    {
        if ($this->request->isAjax()) {
            // 接收删除操作的状态 0 单条 1 多条
            // $state = $this->request->param('state');

            $ids = $this->request->param('id', '');

            // $ids = !empty($ids) ? rtrim($ids,',') : '';

            $idArr = explode(',', $ids);

            $idArr = array_filter($idArr);

            // 封装返回信息的数组
            $result = [
                'code' => 0,
                'msg' => '未知信息',
            ];

            if (empty($idArr)) {
                $result['msg'] = '请选择要删除的用户';
                return json($result);
            }

            // 防止删除当前登录的用户
            if (in_array($this->LoginAdmin['id'], $idArr)) {
                $result['msg'] = '不能选中当前登录的用户，请重新选择';
                return json($result);
            }

            $res = $this->CateModel->whereIn('id', $idArr)->delete();

            if ($res === FALSE) {
                $result['msg'] = '删除失败';
                return json($result);
            } else {
                $result['code'] = 1;
                $result['msg'] = '删除成功';
                return json($result);
            }
        }
    }

    // 单个删除
    public function del()
    {
        if ($this->request->isAjax()) {
            // 接收删除操作的状态 0 单条 1 多条
            // $state = $this->request->param('state');

            $ids = $this->request->param('id', '');

            // $ids = !empty($ids) ? rtrim($ids,',') : '';

            $idArr = explode(',', $ids);

            $idArr = array_filter($idArr);

            // 封装返回信息的数组
            $result = [
                'code' => 0,
                'msg' => '未知信息',
            ];

            if (empty($idArr)) {
                $result['msg'] = '请选择要删除的用户';
                return json($result);
            }

            // 防止删除当前登录的用户
            if (in_array($this->LoginAdmin['id'], $idArr)) {
                $result['msg'] = '不能选中当前登录的用户，请重新选择';
                return json($result);
            }

            $res = $this->CateModel->whereIn('id', $idArr)->delete();

            if ($res === FALSE) {
                $result['msg'] = '删除失败';
                return json($result);
            } else {
                $result['code'] = 1;
                $result['msg'] = '删除成功';
                return json($result);
            }
        }
    }
}
